//
//  DWDStatusCell.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/12.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit
let DWDStatusMargin:CGFloat = 12
let DWDStatusHeight:CGFloat = 35

let DWDPicViewMargin:CGFloat = 10
let DWDPicViewWidth:CGFloat = 90
let DWDPicViewCount:CGFloat = 3
let DWDPicViewMaxWidth:CGFloat = DWDPicViewCount * DWDPicViewWidth + (DWDPicViewCount - 1) * DWDPicViewMargin
class DWDStatusCell: UITableViewCell {
    
    var picViewWidthCons:NSLayoutConstraint?
    var picViewHeightCons:NSLayoutConstraint?
    var picViewTopCons:NSLayoutConstraint?
    var statusModel:DWDStatusModel? {
        didSet{
            topView.statusModel = statusModel
            contentLabel.text = statusModel?.status.text
            
//            赋值的时候那到picView得Bounds
            picView.statusModel = statusModel
            //            根据bounds更改约束
            picViewWidthCons?.constant = picView.bounds.width
            picViewHeightCons?.constant = picView.bounds.height
            //            让有图和无图的约束不一样
            picViewTopCons?.constant = statusModel?.thumbnailUrls?.count == 0 ? 0 :DWDStatusMargin
            bottomView.statusModel = statusModel
        }
    }
    func rowHeight(statusModel:DWDStatusModel) ->CGFloat {
//    约束设置完成之后设置行高
    self.statusModel = statusModel
//     更新约束
    layoutIfNeeded()
//     返回行高
    return CGRectGetMaxY(bottomView.frame)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUpUI() {
  
        let topSepView = UIView()
        topSepView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        contentView.addSubview(topSepView)
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(picView)
        contentView.addSubview(bottomView)
        let width = UIScreen.mainScreen().bounds.width
        topSepView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSepView, size:CGSize(width:SCREENW , height: CGFloat(DWDStatusHeight + DWDStatusMargin)))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: DWDStatusMargin, y: DWDStatusMargin))
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: picView, size: CGSize(width: width, height: 44), offset: CGPoint(x: -DWDStatusMargin, y: DWDStatusMargin))
    }

    private lazy var topView:DWDStatusTopView = DWDStatusTopView()
    lazy var contentLabel:UILabel = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: CGFloat(SCREENW) - CGFloat(2 * DWDStatusMargin))
    lazy var picView:DWDPicView = DWDPicView()
    lazy var bottomView: DWDStatusBottomView = DWDStatusBottomView()
}
